#ifndef UDPRECEIVE_H
#define UDPRECEIVE_H

#include <QObject>
#include <QtNetwork>

typedef struct _ANGLE_TYPE_
{
    char head[4];
    short initCode; // 1 初始化码值 无减速比
    short addupCode; // 2 累计码值   无减速比
    float angleInit; // 4 angle init     float强制转换   有减速比
    float angleCurr; // 6 angle cur      float强制转换   有减速比
    uint16_t asCurrCode; // 3 as5600 cur
    uint16_t step; // 8 step 减速比
    uint32_t time;
}S_AngleType;

typedef struct _ANGLE_SLOW_TYPE_
{
    char head[4];
    short px; // 1 初始化码值 无减速比
    short py; // 2 累计码值   无减速比
    short vx; // 4 angle init     float强制转换   有减速比
    short vy; // 6 angle cur      float强制转换   有减速比
    short rx; // 3 as5600 cur
    short ry; // 8 step 减速比
    uint32_t time;
}S_AngleSlowType;

typedef struct _ANGLE_REPORT_TYPE_
{
    char head[4];
    float p;
    float v;
    float r;
    uint32_t time;
}S_AngleReportType;

typedef struct _COMMAND_TYPE_
{
    char head[4];
    uint8_t typelocate;     // running mode for locate  :COMMAND_LOCATE_TYPE
    uint8_t typespeed;      // running mode for speed   :COMMAND_SPEED_TYPE
    uint16_t res;
    float l1;       // prime axis for COMMAND_LOCATE_AXIS ;x for COMMAND_LOCATE_LOCA
    float l2;       // vice axis for type1  ;y for type2
    float angler;   // rotation axis
    float anglespeed;    // speed for angle  x1°
    float anglems;       // speed for angle  x0.1cm/s=x1mm/s
} S_COMMAND_TYPE;

class UdpReceive : public QObject
{
    Q_OBJECT
public:
    explicit UdpReceive(QObject *parent = nullptr);
    ~UdpReceive();

    bool GetConnectStatus();

signals:
    void SnRequestConnect(QHostAddress udpAddr, int udpPort, QHostAddress multiAddr,
                          QNetworkInterface multiInterface, bool isUseInterface);

    void SnConnectSuccess();
    void SnDisconnect();
    void SnSendRecCount(int);
    void SnSendAngleResult(S_AngleType);
    void SnSendSlowAngleResult(S_AngleSlowType);
    void SnSendRepoAngleResult(S_AngleReportType);

private:
    QUdpSocket *mUdpSocketAll;
    bool isConnected;

    // 处理接收数据
    QByteArray mUdpRecArrayBuf;
    int mUdpMultiRecCounter;

    // 处理保存数据
    S_AngleType mAngleTypeBuf;
};

#endif // UDPRECEIVE_H
